$(document).ready(function() {
    console.log("url=",url);
    var table = $('#institution_table').DataTable({
        "jQueryUI":true,
        "paging": true,// 启用分页
        "lengthMenu":[10,20],// 每页显示条目数选项
        "pageLength": 10,// 默认每页显示条目数
        "searching": true, //启用搜索
        "ordering": true, // 启用排序
        "order": [[0, "asc"]], // 默认按第一列升序排序
        "responsive": true, //响应式
        "oLanguage":{ //语言转换
            "sInfo": "显示第 _START_ 至 _END_ 项结果，共_TOTAL_ 项",
            "sLengthMenu": "每页显示 _MENU_ 项结果",
            "oPaginate": {
                "sFirst": "首页",
                "sPrevious": "前一页",
                "sNext": "后一页",
                "sLast": "尾页"
            }
        },
        "columnDefs": [{
            "targets": -1, // 最后一列
            "data": null,
            "orderable": false, // 禁止排序
            "searchable": false, // 禁止搜索
            "defaultContent": "", // 默认内容为空
            "className": "dt-center", // DataTables内置的居中类
            "render": function(data, type, row, meta){
                return `
                        <div class="action-buttons">
                            <button class="btn-approved" title="通过" data-id="${row[0]}">
                                通过
                            </button>
                            <button class="btn-reject" title="否决" data-id="${row[0]}">
                                否决
                            </button>
                        </div>
                    `;
            }
        }],
        "ajax":{
            type:'get',
            url : url+"getInstitutions",
            header: {
                'content-type': 'application/json',
                'authorization':"Bearer " + token
            },
            data:{
                "institution_id":"111",
                "is_admin": "true"
            },
            "dataSrc": "data.institutions"
        },
        "columns": [
            {data: "Name"},
            {data: "Type"},
            {data: "District"},
            {
                data: "CreatedAt",
                render: function(data) {
                    return new Date(data).toLocaleString(); // 格式化为本地日期时间
                }
            },
            {defaultContent: ""},
            {data: "AdminName"},
            {data: "AdminPhoneNum"},
            {
                data: "CreatedAt",
                render: function(data) {
                    return new Date(data).toLocaleString(); // 格式化为本地日期时间
                }
            },
            {
                data: "State",
                "render": function(data) {
                    const statusMap = {
                        "0": "待审核",
                        "1": "已通过",
                        "2": "已否决"
                    };
                    return statusMap[data] || data;
                }
            },
            {defaultContent: "" }
        ]
    });
    // 通过按钮点击事件
    $('#institution_table').on('click', '.btn-approved', function() {
        var data = table.row($(this).parents('tr')).data();
        $("#titleText").html("审核机构-通过");
        var contentHtml = `
                <div class="deleteConfirm">
                    <p>您确定要将【<span></span>】审核为“通过”吗？</p>
                    <button class="closeBtn">取消</button>
                    <button class="confirmBtn">确认</button>
                </div>`;
        $(".popupContent").html(contentHtml);
        $(".closeBtn").off("click").on('click',function (){
            $(".popup").css("display", "none");
        });
        $(".confirmBtn").off("click").on('click',function (){
            $(".popup").css("display", "none");
        });
        $(".popup").css("display", "block");
    });

    // 编辑按钮点击事件
    $('#institution_table').on('click', '.btn-reject', function() {
        var data = table.row($(this).parents('tr')).data();
        alert('编辑ID: ' + data[0]);
    });
});